import { resolve } from 'path'
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import viteCompression from 'vite-plugin-compression'
import { visualizer } from 'rollup-plugin-visualizer'
import AutoImport from 'unplugin-auto-import/vite'
import Less2CssVariablePlugin from 'antd-less-to-css-variable'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const alias = {
    '~': `${resolve(__dirname, './')}`,
    '@/': `${resolve(__dirname, 'src')}/`
  }

  return {
    server: {
      port: "84",
      proxy: {
        '/api': {
          target: "http://127.0.0.1:83",
          ws: false,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    resolve: {
      alias
    },
    build: {
      // sourcemap: true,
      manifest: true,
      rollupOptions: {
        output: {
          manualChunks: {
            echarts: ['echarts'],
            'ant-design-vue': ['ant-design-vue'],
            vue: ['vue', 'vue-router', 'pinia']
          }
        }
      },
      chunkSizeWarningLimit: 1000
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue']
      }),
      viteCompression(),
      vueJsx(),
      visualizer()
    ],
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          plugins: [new Less2CssVariablePlugin()]
        }
      }
    },
    optimizeDeps: {}
  }
})
